package com.ycrlkj.android.modulescore.main.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiException;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiExceptionHandler;
import com.ycrlkj.android.modulespublic.common.net.exception.ErrorConsumer;
import com.ycrlkj.android.modulespublic.common.net.transformer.ResponseTransformer;
import com.ycrlkj.android.modulescore.main.BR;
import com.ycrlkj.android.modulescore.main.R;
import com.ycrlkj.android.modulescore.main.apiservice.VehicleApiService;
import com.ycrlkj.android.modulescore.main.databinding.ActivityVehicleListBinding;
import com.ycrlkj.android.modulescore.main.entity.Vehicle;
import com.ycrlkj.android.modulespublic.common.base.DataBindingRecyclerViewAdapter;
import com.ycrlkj.android.modulespublic.common.constant.RoutePath;
import com.ycrlkj.android.modulespublic.common.utils.xpopup.XPopupUtil;
import com.ycrlkj.android.modulespublic.common.widget.titlebar.MTitleBar;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
@Route(path = RoutePath.PAGE_VEHICLE_LIST)
public class VehicleListActivity extends AppCompatActivity {

    @Inject
    VehicleApiService vehicleApiService;

    private ActivityVehicleListBinding binding;

    private DataBindingRecyclerViewAdapter<Vehicle> adapter;

    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityVehicleListBinding.inflate(getLayoutInflater());

        setContentView(binding.getRoot());

        initTitleBar();
        initAdapter();

        String userId = SPStaticUtils.getString("userId");
        getAllByUserId(userId);
    }

    public void initAdapter() {
        recyclerView = binding.recyclerView;
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        adapter = new DataBindingRecyclerViewAdapter<>(this, R.layout.item_vehicle, BR.vehicle, new DataBindingRecyclerViewAdapter.OnItemClickListener<Vehicle>() {
            @Override
            public void onItemClickListener(Vehicle item, int position) {
                String id = item.getId();

                if (TextUtils.isEmpty(id)) {
                    ToastUtils.showLong("未找到此车辆, 请刷新页面");
                } else {
                    ARouter.getInstance().build(RoutePath.PAGE_VEHICLE_DETAIL).withString("id", id).withBoolean("editEnabled", true).navigation();
                }
            }
        });
        recyclerView.setAdapter(adapter);
    }


    @SuppressLint("CheckResult")
    public void getAllByUserId(String userId) {
        LoadingPopupView loadingPopupView = XPopupUtil.showLoading(this);
        XPopupUtil.hideLoadingDelay(loadingPopupView);

        vehicleApiService.getAllByUserId(userId)
                .compose(ResponseTransformer.obtain())
                .subscribe(vehicles -> {
                    adapter.add(vehicles);
                    XPopupUtil.hideLoading(loadingPopupView);
                }, new ErrorConsumer() {
                    @Override
                    protected void error(ApiException e) {
                        XPopupUtil.hideLoading(loadingPopupView);
                        ApiExceptionHandler.doWith(VehicleListActivity.this, e);
                    }
                });
    }


    public void initTitleBar() {
        MTitleBar mTitleBar = binding.mTitleBar;
        mTitleBar.setBtnEvents(new MTitleBar.BtnEvents() {
            @Override
            public void setBtnLeftOnClickListener() {
                finish();
            }

            @Override
            public void setBtnRightOnClickListener() {
                ARouter.getInstance().build(RoutePath.PAGE_VEHICLE_REGISTER).navigation();
            }
        });
    }
}